﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Collections;
using Telerik.Web.UI;
using System.Drawing;
using System.Globalization;

namespace BVVD_Website.userRight
{
    public partial class frmGroupUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                grvGroupUser.Culture = new CultureInfo("vi-VN");
                grvGroupUser.Rebind();

                BVVDDataContext db = new BVVDDataContext();

                var listNHOM = (from lh in db.LST_NHOMNSDs
                                where lh.TRANGTHAI.Equals("1")
                                select new
                                {
                                    MANHOM = lh.MANHOM,
                                    TENNHOM = lh.TENNHOM
                                }).ToList();
                listNHOM.Add(new { MANHOM = "1", TENNHOM = "Tất cả" });
                ddlGROUP.DataSource = listNHOM.OrderBy(a => a.MANHOM);
                ddlGROUP.DataTextField = "TENNHOM";
                ddlGROUP.DataValueField = "MANHOM";
                ddlGROUP.DataBind();
                fillgrvGroupUser();
                grvGroupUser.DataBind();

            }
        }

        private void fillgrvGroupUser()
        {

            BVVDDataContext db = new BVVDDataContext();
            var listNSD = (from nsd in db.LST_NSDs
                           join gr in db.F_NHOM_NSDs on nsd.MANSD equals gr.MANSD into temp1
                           from tmp1 in temp1.DefaultIfEmpty()
                           select new
                           {
                               RECID = nsd.MANSD,
                               MANSD = nsd.MANSD,
                               TENNSD = nsd.TENNSD,
                               MANHOM = tmp1.MANHOM == null ? "" : tmp1.MANHOM,
                               TENNHOM = tmp1.LST_NHOMNSD.TENNHOM,
                               NGAYGAN = tmp1.NGAYGAN,
                               TRANGTHAI = tmp1.TRANGTHAI,
                               GHICHU = tmp1.GHICHU
                           }).ToList();

            if (ddlGROUP.SelectedIndex >= 0 && !ddlGROUP.SelectedValue.Equals("1"))
            {
                grvGroupUser.DataSource = listNSD.Where(a => a.MANHOM.Equals(ddlGROUP.SelectedValue));

            }
            else
            {
                grvGroupUser.DataSource = listNSD;
            }

        }


        protected void grvGroupUser_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvGroupUser();
        }
        protected void ddlGROUP_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvGroupUser();
            grvGroupUser.DataBind();
        }


        protected void grvGroupUser_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                BVVDDataContext db = new BVVDDataContext();
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                //RadTextBox txtMALH = editFormItem.FindControl("txtMALH") as RadTextBox;
                //Label lblTENLH = editFormItem.FindControl("lblMANSD") as Label;

                RadDropDownList ddlMANHOM = editFormItem.FindControl("ddlMANHOM") as RadDropDownList;

                var list = (from n in db.LST_NHOMNSDs
                            where n.TRANGTHAI.Equals("1")
                            select n).ToList();
                ddlMANHOM.DataSource = list;
                ddlMANHOM.DataValueField = "MANHOM";
                ddlMANHOM.DataTextField = "MANHOM";
                ddlMANHOM.DataBind();

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {

                }
                else
                {
                    //Label lblTRANGTHAI = ;
                    rblTRANGTHAI.SelectedValue = editFormItem["TRANGTHAI"].Text;
                    ddlMANHOM.SelectedValue = editFormItem["MANHOM"].Text;


                }

            }


        }

        protected void grvGroupUser_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string mansd = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["MANSD"].ToString();

            F_NHOM_NSD user = (from nsd in db.F_NHOM_NSDs
                               where nsd.MANSD.Equals(mansd)
                               select nsd).SingleOrDefault();
            if (user != null)
            {                
                user.MANHOM = (editedItem.FindControl("ddlMANHOM") as RadDropDownList).SelectedValue;
                user.MANSD = (editedItem.FindControl("lblMANSD") as Label).Text;
                user.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
                user.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
                user.NGAYGAN = DateTime.Now;
                try
                {
                    db.SubmitChanges();
                    rnAlert.Text = "Cập nhật thành công gán nhóm có mã <font color='red'> " + user.MANSD + "</font>";
                }
                catch (Exception ex)
                {
                    rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
                }
            }
            else
            {
                F_NHOM_NSD g_user = new F_NHOM_NSD
                {
                    MANHOM = (editedItem.FindControl("ddlMANHOM") as RadDropDownList).SelectedValue,
                    MANSD = (editedItem.FindControl("lblMANSD") as Label).Text,
                    GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                    TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue,
                    NGAYGAN = DateTime.Now

                };
                db.F_NHOM_NSDs.InsertOnSubmit(g_user);
                try
                {
                    db.SubmitChanges();
                    rnAlert.Text = "Cập nhật thành công gán nhóm có mã <font color='red'> " + g_user.MANSD + "</font>";
                }
                catch (Exception ex)
                {
                    rnAlert.Text = "Có lỗi xảy ra. \n <br> Nguyên nhân : " + ex.Message;
                }


            }

            rnAlert.Show();
        }

    }
}